// components/MyHomeNav/MyHomeNav.js
const app = getApp();
const db = wx.cloud.database();
const collection = "home";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    active: {
      type: Number
    },
    nav: {
      type: Array,
      value: []
    }
  },
  options: {
    "styleIsolation": 'apply-shared',
  },

  /**
   * 组件的初始数据
   */
  data: {

  },
  observers: {
    'active,nav': function (active, nav) {
      console.log(active, nav);
      if (nav.length) {
        db.collection(collection).where({
          id: nav[active].id
        }).get().then(res => {
          if (res?.data?.length) {
            this.triggerEvent("changeShowData", {
              showData: res.data[0].list
            });
          } else {
            wx.request({
              url: `${app.globalData.baseUrlData}/yunyuedu${this.data.nav[active].url}`,
              method: "GET",
              success: (res) => {
                this.triggerEvent("changeShowData", {
                  showData: res.data.data.list
                });
                wx.cloud.callFunction({
                  name: "addHomeData",
                  data: {
                    homeData: {
                      ...nav[active],
                      list: res.data.data.list
                    }
                  }
                })
              }
            });
          }
        });
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    changeActive(e) {
      if (e.target.dataset.index == 5) {
        wx.showToast({
          icon: "none",
          title: '敬请期待',
        })
        return
      }
      this.triggerEvent("changeActive", {
        active: e.target.dataset.index
      });
    },
  },
})